package org.Script;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineFactory;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import javax.swing.*;
import java.util.List;

public class TestJS {
    public static void main(String[] args) throws ScriptException {
        List<ScriptEngineFactory> factories = new ScriptEngineManager().getEngineFactories();
        for (ScriptEngineFactory factory : factories) {
            System.out.println(factory.getEngineName());
            ScriptEngine engine = new ScriptEngineManager().getEngineByName(factory.getEngineName());
            System.out.println("has engine " + (engine != null));
        }
        ScriptEngine engine = new ScriptEngineManager().getEngineByName("Graal.js");
        if (engine == null) {

            System.out.println("No JavaScript engine found");
            return;
        }
        engine.put("x", 1);
        engine.put("y", 2);
        engine.eval("z = x + y");
        System.out.println("z: " + engine.get("z"));
        Object result = engine.eval("z + 100");
        System.out.println("result: " + result);

        ScriptEngineFactory factory =
                new ScriptEngineManager().getEngineFactories().get(0);
//        factory.getParameter("THREADING");
        // null: 并发执行不安全
        //
        System.out.println("THREADING: " + factory.getParameter("THREADING"));
//        System.out.println("THREADING: " + factory.getParameter("THREADING"));
//        System.out.println("THREADING: " + factory.getParameter("THREADING"));
//        engine.put("k", 1728);
//        engine.put("b", new JButton());
//        engine.eval("k = k * b.getWidth()");
//        result = engine.eval("k");
//        System.out.println("result: " + result);

        JButton button = new JButton();
        button.getWidth();
    }
}
